/******************************************************************************
//             INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//        Copyright (c) 2000-2001 Intel Corporation. All Rights Reserved.
//
//  Description:
//      Intel(R) JPEG library for X platform 
//
********************************************************************************/
#include "_ijx.h"

extern const Ipp8u _nIZigZagIndex[64];

/******************************************************************************
// Name:            _InitScanParam_BLS
// Description:     Intialize scan parameters for baseline sequential mode
// Input Arguments:
//     pEncodeState: Points to IJENCODESTATE data structure.
// Output Arguments:
//                   NONE       
// Returns:          
//                  1: Countinue
//                  0: End
//                 -1: Error
//                     
//                  
******************************************************************************/
static IJXRESULT _InitScanParam_BLS(IJENCODESTATE *pEncodeState)
{
    int i;
    IJSCANSPEC *pScanSpec=&(pEncodeState->scanSpec);
    pScanSpec->ss=0;
    pScanSpec->se=63;
    pScanSpec->ah=0;
    pScanSpec->al=0;
    pScanSpec->nNumComps=pEncodeState->imageSpec.nNumComponent;
    for (i=0; i<pScanSpec->nNumComps; i++) {
        pScanSpec->ppCompSpec[i]=&(pEncodeState->compSpec[i]);
        pEncodeState->dcPred[i]=0;
    }

    return IJX_RTCODE_S_EOF;
}

/******************************************************************************
// Name:            _InitScanParam_P
// Description:     Intialize scan parameters for JPEG progressive mode
// Input Arguments:
//     pEncodeState: Points to IJENCODESTATE data structure.
// Output Arguments:
//                   NONE       
// Returns:          
//                  1: Countinue
//                  0: End
//                 -1: Error
//                     
//                  
******************************************************************************/
static IJXRESULT _InitScanParam_P(IJENCODESTATE *pEncodeState)
{
    int i;
    IJXRESULT  rtCode = IJX_RTCODE_S_CONTINUE;
    IJSCANSPEC *pScanSpec=&(pEncodeState->scanSpec);

    pEncodeState->progSpec.nEOBRun=0;
    pEncodeState->progSpec.nValidBits=0;

    switch (pEncodeState->imageSpec.nColorMode) {
        case IIP_CM_GRAY:
            switch (pEncodeState->counter.nScanCounter) {
                case 1: //DC
                    pScanSpec->ss=0;
                    pScanSpec->se=0;
                    pScanSpec->ah=0;
                    pScanSpec->al=1;
                    break;
                case 2: //AC
                    pScanSpec->ss=1;
                    pScanSpec->se=5;
                    pScanSpec->ah=0;
                    pScanSpec->al=2;
                    break;
                case 3: //AC
                    pScanSpec->ss=6;
                    pScanSpec->se=63;
                    pScanSpec->ah=0;
                    pScanSpec->al=2;
                    break;
                case 4: //AC
                    pScanSpec->ss=1;
                    pScanSpec->se=63;
                    pScanSpec->ah=2;
                    pScanSpec->al=1;
                    break;
                case 5: //DC
                    pScanSpec->ss=0;
                    pScanSpec->se=0;
                    pScanSpec->ah=1;
                    pScanSpec->al=0;
                    break;
                case 6: //AC
                    pScanSpec->ss=1;
                    pScanSpec->se=63;
                    pScanSpec->ah=1;
                    pScanSpec->al=0;
                    rtCode = IJX_RTCODE_S_EOF;
                    break;
                default:
                    rtCode = IJX_RTCODE_ERROR;
                    break;
            }
            pScanSpec->nNumComps=1;
            pScanSpec->ppCompSpec[0]=&(pEncodeState->compSpec[0]);
            pEncodeState->dcPred[0]=0;
            break;
        //case IIP_CM_RGB:
        case IIP_CM_BGR:
        case IIP_CM_BGR565:
        case IIP_CM_BGR555:
            switch (pEncodeState->counter.nScanCounter) {
                case 1: //DC
                    pScanSpec->ss=0;
                    pScanSpec->se=0;
                    pScanSpec->ah=0;
                    pScanSpec->al=1;
                    pScanSpec->nNumComps=pEncodeState->imageSpec.nNumComponent;
                    for (i=0; i<pScanSpec->nNumComps; i++) {
                        pScanSpec->ppCompSpec[i]=&(pEncodeState->compSpec[i]);
                        pEncodeState->dcPred[i]=0;
                    }
                    break;
                case 2: //AC 0
                    pScanSpec->ss=1;
                    pScanSpec->se=5;
                    pScanSpec->ah=0;
                    pScanSpec->al=2;
                    pScanSpec->nNumComps=1;
                    pScanSpec->ppCompSpec[0]=&(pEncodeState->compSpec[0]);
                    break;
                case 3: //AC 2
                    pScanSpec->ss=1;
                    pScanSpec->se=63;
                    pScanSpec->ah=0;
                    pScanSpec->al=1;
                    pScanSpec->nNumComps=1;
                    pScanSpec->ppCompSpec[0]=&(pEncodeState->compSpec[2]);
                    break;
                case 4: //AC 1
                    pScanSpec->ss=1;
                    pScanSpec->se=63;
                    pScanSpec->ah=0;
                    pScanSpec->al=1;
                    pScanSpec->nNumComps=1;
                    pScanSpec->ppCompSpec[0]=&(pEncodeState->compSpec[1]);
                    break;
                case 5: //AC 0
                    pScanSpec->ss=6;
                    pScanSpec->se=63;
                    pScanSpec->ah=0;
                    pScanSpec->al=2;
                    pScanSpec->nNumComps=1;
                    pScanSpec->ppCompSpec[0]=&(pEncodeState->compSpec[0]);
                    break;
                case 6: //AC 0
                    pScanSpec->ss=1;
                    pScanSpec->se=63;
                    pScanSpec->ah=2;
                    pScanSpec->al=1;
                    pScanSpec->nNumComps=1;
                    pScanSpec->ppCompSpec[0]=&(pEncodeState->compSpec[0]);
                    break;
                case 7://DC 
                    pScanSpec->ss=0;
                    pScanSpec->se=0;
                    pScanSpec->ah=1;
                    pScanSpec->al=0;
                    pScanSpec->nNumComps=pEncodeState->imageSpec.nNumComponent;
                    for (i=0; i<pScanSpec->nNumComps; i++) {
                        pScanSpec->ppCompSpec[i]=&(pEncodeState->compSpec[i]);
                        pEncodeState->dcPred[i]=0;
                    }
                    break;
                case 8: //AC 2
                    pScanSpec->ss=1;
                    pScanSpec->se=63;
                    pScanSpec->ah=1;
                    pScanSpec->al=0;
                    pScanSpec->nNumComps=1;
                    pScanSpec->ppCompSpec[0]=&(pEncodeState->compSpec[2]);
                    break;
                case 9: //AC 1
                    pScanSpec->ss=1;
                    pScanSpec->se=63;
                    pScanSpec->ah=1;
                    pScanSpec->al=0;
                    pScanSpec->nNumComps=1;
                    pScanSpec->ppCompSpec[0]=&(pEncodeState->compSpec[1]);
                    break;
                case 10: //AC 0
                    pScanSpec->ss=1;
                    pScanSpec->se=63;
                    pScanSpec->ah=1;
                    pScanSpec->al=0;
                    pScanSpec->nNumComps=1;
                    pScanSpec->ppCompSpec[0]=&(pEncodeState->compSpec[0]);
                    rtCode = IJX_RTCODE_S_EOF;
                    break;
                default:
                    rtCode = IJX_RTCODE_ERROR;
                    break;
            } break;
        default:
            rtCode = IJX_RTCODE_ERROR;
            break;

    }

    return rtCode;
}

/******************************************************************************
// Name:            ijInitScanSpecFwd
// Description:     Encode Scan.
// Input Arguments:
//    pEncodeState: Identifies IJENCODESTATE strcuture.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//                  IJX_RTCODE_S_EOF
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_F_INSUFFICIENTBUF
//                  IJX_RTCODE_E_ERRPARAM
//                  
******************************************************************************/
IJXRESULT ijInitScanSpecFwd(IJENCODESTATE *pEncodeState)
{
    IJXRESULT rtCode;

    pEncodeState->nHMCUCurPos=0;
    pEncodeState->nVMCUCurPos=0;

    /* MCU Counter */
//    pEncodeState->counter.nFrameMCUCounter+=pEncodeState->counter.nScanMCUCounter;
//    pEncodeState->counter.nScanMCUCounter=0;
    /* Scan Conter */
    pEncodeState->counter.nScanCounter++;
    /* Restart Interval Counter */
    pEncodeState->counter.nDRICounter=pEncodeState->nRstInterval;
    pEncodeState->counter.nRSTCounter=0;

    if (IJX_MODE_PROGRESSIVE&pEncodeState->nJPEGCodeMode) {
        rtCode=_InitScanParam_P(pEncodeState);
    } else {
        rtCode=_InitScanParam_BLS(pEncodeState);
    }

    return rtCode;
}

/* EOF */
